﻿/*
%include "/projects/hsieh_project/proj_201809/code_1_data/data_0_read_raw.sas" /source2;

This sas file
1) Subsets the data
2) Adds variables of industry-firm's rank and percentile based on employment (0=more employment, 1=less employment)

/* REDACTED
Certain industries are dropped. Code is redacted. */

*/


/*
--------------------------------------------------------------------------------
Source files
*/

%include "/projects/data_commons/lbd_code/lbd_m_merge_ch_ind_sect.sas" /source2;
%include "/projects/data_commons/lbd_code/lbd_m_merge_runif.sas" /source2;
%include "/projects/hsieh_project/code_0_general/m_perc_by_var.sas" /source2;

/*
--------------------------------------------------------------------------------
Merge with sector and random variable
*/
%merge_ch_ind_sect(ds_in=lbd, ds_out=lbd);
%merge_runif(ds_in=lbd, ds_out=lbd, l_runif=runif_1 runif_2);

/* Note that the ch_ind-sect crosswalk now differentiates ch_ind codes that are changed and that are not */

/*
--------------------------------------------------------------------------------
Subset
*/

data lbd;
  set lbd;
  /* REDACTED 
  Certain fk_naics are dropped */
  /* Remove out of scope fk_naics */
run;

/* REDACTED
----------------------------------------
Drop observations whose ch_ind are not mapped from fk_naics
*/

/* REDACTED
----------------------------------------
Drop selected industries */

/* Also drop sectors (and missing)
Drop missing MSA
*/


data lbd;
  set lbd;
  if ch_ind ~= .;
run;

data lbd;
  set lbd;
  if msa ~= .;
run;

data check_sect;
  set lbd;
  if sector = .;
run;

data lbd;
  set lbd;
/*###### COMMENT REMOVED ######*/
run;

/*
--------------------------------------------------------------------------------
Add variables of industry-firm's rank and percentile based on employment (0=more employment, 1=less employment)
*/

%m_perc_by_var(ds_in=lbd, ds_out=lbd_indf, var_in=worker, var_by=%bquote(year ch_ind), var_unit=firmnum, var_out=indf, var_runif=runif_1);


proc sort data=lbd;
  by year firmnum ch_ind;
run;

proc sort data=lbd_indf;
  by year firmnum ch_ind;
run;

data lbd;
  merge lbd lbd_indf;
  by year firmnum ch_ind;
run;

proc sort data=lbd;
  by year lbdid;
run;

/* End of SAS file */
